package Other;

import DataConnectionInterface.ConnectionPool;
import DataConnectionInterface.GameDataConnectionInterface;
import Helper.GameException;

// class for things we view through pages
public abstract class Pager {
	
	protected int entriesPerPage;
	protected int pageNumber = 1;
	
	public void init(int entriesPerPage) {
		ConnectionPool pool = null;
		GameDataConnectionInterface con = null;
		try {
			pool = ConnectionPool.getInstance();
			con = pool.getConnection();
			this.entriesPerPage = entriesPerPage;
			RefreshPage(con);
		} catch (GameException e) {
		}
		finally {
			pool.returnConnection(con);
		}
	}
	
	public void toNextPage() {
		ConnectionPool pool = null;
		GameDataConnectionInterface con = null;
		try {
			pool = ConnectionPool.getInstance();
			con = pool.getConnection();
			getNextPage(con);
			pool.returnConnection(con);
		} catch (GameException e) {
			if(pool != null && con != null) pool.returnConnection(con);
		}
	}

	public void toPreviousPage() {
		
		ConnectionPool pool = null;
		GameDataConnectionInterface con = null;
		if(pageNumber > 1){
			try {
				pool = ConnectionPool.getInstance();
				con = pool.getConnection();
				getPreviousPage(con);
				pool.returnConnection(con);
				
			} catch (GameException e) {
				if(pool != null && con != null) pool.returnConnection(con);
			}
		}
	}

	protected abstract void getNextPage(GameDataConnectionInterface con) throws GameException;
	protected abstract void getPreviousPage(GameDataConnectionInterface con) throws GameException;
	protected abstract void RefreshPage(GameDataConnectionInterface con) throws GameException;

}
